import { a_tank_actionproperty, tank_actions} from "../commdata";
import {A_soldier} from "./a_soldier";
import index from "strip-ansi";
import g1 from "../sound/g1.wav";
import {Basebio} from "./basebio";
import {Obj_class} from "./obj_class";
import {allobjects, ReleaseResource, ReStortObjects} from "../comm";

export class Tank extends Basebio{

     constructor(_name:string,_live:number,_livelimit:number,_x:number,_y:number,_demage:number,_shootrange:number,_team:number,_speed:number) {
         super();
         this.name = _name;
         this.live = _live;
         this.livelimit = _livelimit;
         this.x = _x;
         this.y = _y;
         this.actioncount = 1;
         this.actionimg = new Image();
         this.fun = "r";
         this.demage = _demage;
         this.shootrange = _shootrange+Math.floor(Math.random() * (50) + 0);
         //this.sdadio.src = g1;
         this.fireimag = new Image();
         this.team=_team;
         this.actrons = tank_actions;
         this.ratewidth=a_tank_actionproperty.ratewidth/1.5;
         this.rateheight=a_tank_actionproperty.rateheight/1.5;
         this.speed=_speed;
     }

    fire=(enemy:Basebio):boolean=> {

        if (this.fireindex < this.firecount) {
            this.fireindex = this.fireindex + 1;
        } else {
            this.fireindex = 0;
            this.isfire = false;
        }

        //射中机率

        let tmpflag = Math.floor(Math.random() * (70 - 0 + 1)) + 0;
        if (tmpflag > 50) {
           // this.sdadio.play();
            if (enemy.live > 0) {
                enemy.live = enemy.live - this.demage;
                if (enemy.live <= 0) {
                    enemy.live = 0;
                    enemy.actionindex = 0;
                    this.killcount = this.killcount + 1;
                    //排序
                    ReStortObjects(allobjects);
                }

            }
            return true;


        }
        return false;
    }

    setTankfire=(enemys:Obj_class[])=>{
        if(this.isstart==false)
        {
            return;
        }
        if(this.fireindex==0) {
            for (let i = 0; i < enemys.length; i++) {
                let enemy:Basebio=enemys[i].objval;
                if (enemy.live > 0 && enemy.team!=this.team) {
                    if (enemy.x < this.x + this.ratewidth + this.shootrange &&
                        enemy.x > this.x + this.ratewidth) {
                        this.isfire=true;
                        this.fun="r";
                        if (enemy.y < this.y - this.rateheight /2) {
                            this.fun = "ur";
                        } else if (enemy.y > this.y + this.rateheight/2) {
                            this.fun = "dr";
                        }
                        if (this.fire(enemy) == true) {
                            return;
                        }

                    } else if (enemy.x > this.x - this.ratewidth - this.shootrange &&
                        enemy.x < this.x - this.ratewidth) {
                        this.isfire = true;
                        this.fun="l";
                        if (enemy.y < this.y - this.rateheight/2) {
                            this.fun = "ul";
                        } else if (enemy.y > this.y + this.rateheight/2) {
                            this.fun = "dl";
                        }
                        if (this.fire(enemy) == true) {
                            return;
                        }
                    }
                }
            }
        }else
        {
            if(this.fireindex<this.firecount) {
                this.fireindex = this.fireindex+1;
            }
            else
            {
                this.fireindex =0;
                this.isfire=false;
            }

        }
    }

    actiondraw = (ptctx: any, max_width: number, max_height: number) => {
        if(this.live>0) {
            if (this.fun == "l") {
                this.x = this.x - this.speed;
            } else if (this.fun == "r") {
                this.x = this.x + this.speed;
            } else if (this.fun == "u") {
                this.y = this.y - this.speed;
            } else if (this.fun == "d") {
                this.y = this.y + this.speed;
            } else if (this.fun == "ul") {
                this.x = this.x - this.speed;
                this.y = this.y - this.speed;
            } else if (this.fun == "ur") {
                this.x = this.x + this.speed;
                this.y = this.y - this.speed;
            } else if (this.fun == "dl") {
                this.x = this.x - this.speed;
                this.y = this.y + this.speed;
            } else if (this.fun == "dr") {
                this.x = this.x + this.speed;
                this.y = this.y + this.speed;
            } else {
                this.x = this.x + this.speed;
            }

            if (this.x < 0) {
                this.actionindex = 0;
                this.fun = "r";
                this.x = 0;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    let tempfun=this.fun;
                    if(this.isfire==true)
                    {
                            //this.sdadio.play();
                            tempfun = this.fun + "_f";
                    }
                    if (tempfun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.x > max_width - this.ratewidth) {
                this.actionindex = 0;
                this.fun = "l";
                this.x = max_width - this.ratewidth;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    let tempfun=this.fun;
                    if(this.isfire==true)
                    {
                        //this.sdadio.play();
                        tempfun = this.fun + "_f";
                    }
                    if (tempfun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.y < 0) {
                this.actionindex = 0;
                this.y = 0;
                this.fun = "d";
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    let tempfun=this.fun;
                    if(this.isfire==true)
                        //if(this.fireindex>1) {
                        //this.sdadio.play();
                        tempfun = this.fun + "_f";
                    //}
                    if (tempfun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.y > max_height - this.rateheight) {
                this.actionindex = 0;
                this.fun = "u";
                this.y = max_height - this.rateheight;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    let tempfun=this.fun;
                    if(this.isfire==true)
                    {
                        //this.sdadio.play();
                        tempfun = this.fun + "_f";
                    }
                    if (tempfun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            }
            this.currantactions = [];
            for (let i = 0; i < this.actrons.length; i++) {
                let tempfun=this.fun;
                if(this.isfire==true)
                {
                    //this.sdadio.play();
                    tempfun = this.fun + "_f";
                }
                if (tempfun == this.actrons[i].actiontype) {
                    let temp = this.actrons[i];
                    this.currantactions.push(temp);
                }
            }
            this.actioncount=this.currantactions.length;
            this.actionimg.src = this.currantactions[this.actionindex].imgsrc;
            ptctx?.drawImage(this.actionimg, 0, 0, this.actionimg.width, this.actionimg.height, this.x, this.y, this.ratewidth, this.rateheight
            );

            if (this.actionindex < this.actioncount - 1)
                this.actionindex = this.actionindex + 1;
            else {
                this.actionindex = 0;
                this.deadfinish=true;
            }
        }
    }

    drawinfo=(ptctx: any)=>{
         if(this.name!="") {
             ptctx.font = "oblique small-caps bold 10px Arial";
             ptctx.fillStyle = "#33FF00";
             ptctx.fillText(this.name, this.x, this.y - 20);
             ptctx.fillText("生命：" + this.live.toString()+"/"+this.livelimit.toString(), this.x, this.y-10);
             ptctx.fillText("杀敌：" + this.killcount.toString(), this.x, this.y);
             ptctx.stroke();
         }
    }

    deaddraw=(ptctx: any)=>{
         // if(this.deadfinish==false)
         // {

             this.currantactions = [];
             for (let i = 0; i < this.actrons.length; i++) {
                 if (this.actrons[i].actiontype == "deadflame") {
                     let temp = this.actrons[i];
                     this.currantactions.push(temp);
                 }
             }
             this.actioncount= this.currantactions.length;
             this.actionimg.src = this.currantactions[this.actionindex].imgsrc;
             ptctx?.drawImage(this.actionimg, 0, 0, this.actionimg.width, this.actionimg.height, this.x-10, this.y-10, this.actionimg.width, this.actionimg.height
             );
             if (this.actionindex < this.actioncount - 1)
                 this.actionindex = this.actionindex + 1;
             else
             {
                 this.actionindex=this.actioncount - 1;
                 this.deadfinish=true;
                 ReleaseResource(this);
             }
         //}
    }
}